package com.lefeng.cockpit.resource.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefeng.cockpit.common.bean.dto.resource.ResourceDTO;
import com.lefeng.cockpit.common.bean.po.resource.ResourcePO;
import com.lefeng.cockpit.resource.dao.ResourceDao;
import com.lefeng.cockpit.resource.dao.mapper.ResourceMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class ResourceDaoImpl implements ResourceDao {

    @Resource
    private ResourceMapper resourceMapper;


    @Override
    public int insert(ResourcePO resourcePO) {
        return resourceMapper.insert(resourcePO);
    }

    @Override
    public Boolean existResource(String fullName, int type) {
        LambdaQueryWrapper<ResourcePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourcePO::getFullName, fullName)
                .eq(ResourcePO::getType, type)
                .last("LIMIT 1"); // Optional, as `LIMIT 1` is handled by `exists`

        return resourceMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public ResourcePO selectById(int pid) {
        return resourceMapper.selectById(pid);
    }

    @Override
    public List<ResourcePO> queryResource(String fullName, int type) {
        LambdaQueryWrapper<ResourcePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourcePO::getFullName, fullName)
                .eq(ResourcePO::getType, type);

        return resourceMapper.selectList(queryWrapper);
    }

    @Override
    public void updateById(ResourcePO parentResource) {
        resourceMapper.updateById(parentResource);
    }

    @Override
    public List<ResourcePO> selectBatchIds(List<Integer> list) {
        return resourceMapper.selectBatchIds(list);
    }

    @Override
    public void deleteIds(Object[] needDeleteResourceIdArray) {
        LambdaQueryWrapper<ResourcePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ResourcePO::getId, needDeleteResourceIdArray);
        resourceMapper.delete(queryWrapper);
    }

    @Override
    public List<Integer> listChildren(int resourceId) {
        // 使用 QueryWrapper
        LambdaQueryWrapper<ResourcePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ResourcePO::getId) // 选择 id 字段
                .eq(ResourcePO::getPid, resourceId); // 添加查询条件

        // 执行查询并返回结果
        return resourceMapper.selectList(queryWrapper)
                .stream()
                .map(ResourcePO::getId) // 提取 id 字段
                .collect(Collectors.toList());
    }

    @Override
    public IPage<ResourcePO> queryResourcePaging(ResourceDTO dto) {
        IPage<ResourcePO> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ResourcePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(dto.getSearchVal() != null, ResourcePO::getAlias, dto.getSearchVal())
                .eq(dto.getType() != null, ResourcePO::getType, dto.getType())
                .eq(dto.getPid() != null, ResourcePO::getPid, dto.getPid())
                .orderByDesc(ResourcePO::getUpdateTime);
        resourceMapper.selectPage(page, queryWrapper);
        page.setTotal(resourceMapper.selectCount(queryWrapper));
        return page;
    }

    @Override
    public List<ResourcePO> selectList() {
        return resourceMapper.selectList(new QueryWrapper<>());
    }
}
